home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JDDEFLTS.C < prev    next >
C/C++ Source or Header  |  1992-07-25  |  7KB  |  164 lines

  1. /*
  2.  * jddeflts.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains optional default-setting code for the JPEG decompressor.
  9.  * User interfaces do not have to use this file, but those that don't use it
  10.  * must know more about the innards of the JPEG code.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /* Default do-nothing progress monitoring routine.
  17.  * This can be overridden by a user interface that wishes to
  18.  * provide progress monitoring; just set methods->progress_monitor
  19.  * after j_d_defaults is done.  The routine will be called periodically
  20.  * during the decompression process.
  21.  *
  22.  * During any one pass, loopcounter increases from 0 up to (not including)
  23.  * looplimit; the step size is not necessarily 1.  Both the step size and
  24.  * the limit may differ between passes.  The expected total number of passes
  25.  * is in cinfo->total_passes, and the number of passes already completed is
  26.  * in cinfo->completed_passes.  Thus the fraction of work completed may be
  27.  * estimated as
  28.  *        completed_passes + (loopcounter/looplimit)
  29.  *        ------------------------------------------
  30.  *                total_passes
  31.  * ignoring the fact that the passes may not be equal amounts of work.
  32.  *
  33.  * When decompressing, the total_passes figure is an estimate that may be
  34.  * on the high side; completed_passes will jump by more than one if some
  35.  * passes are skipped.
  36.  */
  37.  
  38. METHODDEF void
  39. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  40. {
  41.   /* do nothing */
  42. }
  43.  
  44.  
  45. /*
  46.  * Reload the input buffer after it's been emptied, and return the next byte.
  47.  * See the JGETC macro for calling conditions.  Note in particular that
  48.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  49.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  50.  * In the present implementation, we insert an EOI marker; this might not be
  51.  * appropriate for non-JFIF file formats, but it usually allows us to handle
  52.  * a truncated JFIF file.
  53.  *
  54.  * This routine can be overridden by the system-dependent user interface,
  55.  * in case the data source is not a stdio stream or some other special
  56.  * condition applies.  Note, however, that this capability only applies for
  57.  * JFIF or similar serial-access JPEG file formats.  The input file control
  58.  * module for a random-access format such as TIFF/JPEG would most likely
  59.  * override the read_jpeg_data method with its own routine.
  60.  */
  61.  
  62. METHODDEF int
  63. read_jpeg_data (decompress_info_ptr cinfo)
  64. {
  65.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  66.  
  67.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  68.                     cinfo->next_input_byte,
  69.                     JPEG_BUF_SIZE);
  70.   
  71.   if (cinfo->bytes_in_buffer <= 0) {
  72.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  73.     cinfo->next_input_byte[0] = (char) 0xFF;
  74.     cinfo->next_input_byte[1] = (char) 0xD9; /* EOI marker */
  75.     cinfo->bytes_in_buffer = 2;
  76.   }
  77.  
  78.   return JGETC(cinfo);
  79. }
  80.  
  81.  
  82.  
  83. /* Default parameter setup for decompression.
  84.  *
  85.  * User interfaces that don't choose to use this routine must do their
  86.  * own setup of all these parameters.  Alternately, you can call this
  87.  * to establish defaults and then alter parameters selectively.  This
  88.  * is the recommended approach since, if we add any new parameters,
  89.  * your code will still work (they'll be set to reasonable defaults).
  90.  *
  91.  * standard_buffering should be TRUE to cause an input buffer to be allocated
  92.  * (the normal case); if FALSE, the user interface must provide a buffer.
  93.  * This option is most useful in the case that the buffer must not be freed
  94.  * at the end of an image.  (For example, when reading a sequence of images
  95.  * from a single file, the remaining data in the buffer represents the
  96.  * start of the next image and mustn't be discarded.)  To handle this,
  97.  * allocate the input buffer yourself at startup, WITHOUT using alloc_small
  98.  * (probably a direct call to malloc() instead).  Then pass FALSE on each
  99.  * call to j_d_defaults to ensure the buffer state is not modified.
  100.  *
  101.  * If the source of the JPEG data is not a stdio stream, override the
  102.  * read_jpeg_data method with your own routine after calling j_d_defaults.
  103.  * You can still use the standard buffer if it's appropriate.
  104.  *
  105.  * CAUTION: if you want to decompress multiple images per run, it's necessary
  106.  * to call j_d_defaults before *each* call to jpeg_decompress, since subsidiary
  107.  * structures like the quantization tables are automatically freed during
  108.  * cleanup.
  109.  */
  110.  
  111. GLOBAL void
  112. j_d_defaults (decompress_info_ptr cinfo, boolean standard_buffering)
  113. /* NB: the external methods must already be set up. */
  114. {
  115.   short i;
  116.  
  117.   /* Initialize pointers as needed to mark stuff unallocated. */
  118.   /* Outer application may fill in default tables for abbreviated files... */
  119.   cinfo->comp_info = NULL;
  120.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  121.     cinfo->quant_tbl_ptrs[i] = NULL;
  122.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  123.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  124.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  125.   }
  126.   cinfo->colormap = NULL;
  127.  
  128.   /* Default to RGB output */
  129.   /* UI can override by changing out_color_space */
  130.   cinfo->out_color_space = CS_RGB;
  131.   cinfo->jpeg_color_space = CS_UNKNOWN;
  132.   /* Setting any other value in jpeg_color_space overrides heuristics in */
  133.   /* jrdjfif.c.  That might be useful when reading non-JFIF JPEG files, */
  134.   /* but ordinarily the UI shouldn't change it. */
  135.   
  136.   /* Default to no gamma correction of output */
  137.   cinfo->output_gamma = 1.0;
  138.   
  139.   /* Default to no color quantization */
  140.   cinfo->quantize_colors = FALSE;
  141.   /* but set reasonable default parameters for quantization, */
  142.   /* so that turning on quantize_colors is sufficient to do something useful */
  143.   cinfo->two_pass_quantize = TRUE;
  144.   cinfo->use_dithering = TRUE;
  145.   cinfo->desired_number_of_colors = 256;
  146.   
  147.   /* Default to no smoothing */
  148.   cinfo->do_block_smoothing = FALSE;
  149.   cinfo->do_pixel_smoothing = FALSE;
  150.   
  151.   /* Allocate memory for input buffer, unless outer application provides it. */
  152.   if (standard_buffering) {
  153.     cinfo->input_buffer = (char *) (*cinfo->emethods->alloc_small)
  154.                     ((size_t) (JPEG_BUF_SIZE + MIN_UNGET));
  155.     cinfo->bytes_in_buffer = 0;    /* initialize buffer to empty */
  156.   }
  157.  
  158.   /* Install standard buffer-reloading method (outer code may override). */
  159.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  160.  
  161.   /* Install default do-nothing progress monitoring method. */
  162.   cinfo->methods->progress_monitor = progress_monitor;
  163. }
  164.